#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import tornado.web
import tornado.ioloop


# https://wanandroid.com/banner/json
# https://www.wanandroid.com/article/list/1/json

class AdvertHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def get(self):
        ret_dic = {'code': 200, 'message': 'ok'}
        banners = [
            {
                "desc": "雷车在手，说走就走",
                "id": 23,
                "imagePath": "https://img.lexus.com.cn/images/is-exterior-2-0523-2880x1480.jpg",
                "isVisible": 1,
                "order": 0,
                "title": "雷车在手，说走就走",
                "type": 0,
                "url": "http://www.lexus.com.cn"
            },
            {
                "desc": "瑞思英语开学季",
                "id": 6,
                "imagePath": "https://material-mm.oss-cn-beijing.aliyuncs.com/rise/kxj_720x360.jpg",
                "isVisible": 1,
                "order": 1,
                "title": "瑞思英语开学季",
                "type": 1,
                "url": "http://www.wanandroid.com/navi"
            },
            {
                "desc": "一起来吃大闸蟹",
                "id": 20,
                "imagePath": "http://bpic.588ku.com/element_origin_min_pic/16/12/15/f427aca011e127e7fb9ec1c77181c881.jpg",
                "isVisible": 1,
                "order": 3,
                "title": "一起来吃大闸蟹",
                "type": 1,
                "url": "https://flutter.cn/"
            },
            {
                "desc": "一起来做个App吧",
                "id": 10,
                "imagePath": "https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png",
                "isVisible": 1,
                "order": 2,
                "title": "一起来做个App吧",
                "type": 1,
                "url": "http://www.wanandroid.com/blog/show/2"
            }
        ]
        ret_dic['data'] = banners
        ret_str = json.dumps(ret_dic)
        self.write(ret_str)


class NewsHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def get(self):
        ret_dic = {'code': 200, 'message': 'ok'}
        page = int(self.get_argument('page', default='0'))
        begin = page * 20
        end = (page+1) * 20
        sql = f'''select mp_name, mid, read_num, like_num, article_name, article_link, 
            from_unixtime(article_create_time, '%Y-%m-%d %H:%i:%S') from wechat_article limit {begin}, {end}'''
        articles = []
        rows = self.application.db.query(sql)
        for row in rows:
            mp_name, author, read_num, like_num, title, link, article_time = row[:]
            article = {
                'mp_name': mp_name,
                'author': author,
                'title': title,
                'link': link,
                'article_time': article_time,
                'read': False,
                'read_num': read_num,
                'like': True,
                'like_num': like_num
            }
            articles.append(article)
        ret_dic['data'] = articles
        ret_str = json.dumps(ret_dic)
        self.write(ret_str)
